﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Shoot : MonoBehaviour {
	
	private float _bulletTime = 0;
	private float _curTime = 0;

	private StatTracker _st;
	private Magazin _mag;

	// Use this for initialization
	void Start () {
		this._st = GameObject.Find("StatTracker").GetComponent<StatTracker>();
		this._mag = GameObject.Find ("TheDude").GetComponent<Magazin>();

		this._bulletTime = 1f / this._st.GetStat("BpS").GetCur();
	}
	
	// Update is called once per frame
	void Update () {
		if (this._mag.GetNumBullets () > 0)
		{
			this._bulletTime = 1f / this._st.GetStat ("BpS").GetCur ();
			this._curTime += Time.deltaTime;

			if (this._curTime >= this._bulletTime) {
				// shoot
				GameObject bullet = Instantiate (Resources.Load ("Bullet")) as GameObject;
				bullet.transform.position = this.transform.FindChild ("Weapon").position;
				this._mag.RemoveBullet ();

				// reset time
				this._curTime = 0;
			}
		} else
			this._curTime = this._bulletTime;
	}
}
